<template>
	<view>
		<!-- 搜索区域 -->
		<view class="searchBox">
			<uni-search-bar @input="input" cancelButton="none" :radius="100"></uni-search-bar>
		</view>
		<!-- 搜索列表 -->
		<view class="search-list" v-if="queryList.length !== 0">
			<view class="search-item" v-for="(item, i) in queryList" :key="i" @click="gotoGoodsDetail(item)">
				<view class="search-goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="18"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史记录 -->
		<view class="search-history-list" v-else>
			<view class="history-title">
				<text class="title-text">搜索历史</text>
				<uni-icons type="trash" size="18" @click="clearHistory"></uni-icons>
			</view>
			<view class="history-items">
				<uni-tag v-for="(item, i) in historys" :key="i" :text="item"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//防抖定时器
				timer:null,
				//查询关键字
				kw:'',
				//查询列表
				queryList:[],
				//搜索历史记录数据
				historyList:[]
			}
		},
		onLoad(){
			this.historyList = uni.getStorageSync("kw")
		},
		methods: {
			input(value){
				clearInterval(this.timer)
				this.timer = setTimeout(()=>{
					this.kw = value
					this.getQueryList()
					this.saveSearchHistory()
				},300)
			},
			async getQueryList(){
				if(this.kw.length === 0){
					this.queryList = []
					return
				}
				const {data:res} = await uni.$http.get("/api/public/v1/goods/qsearch",{query:this.kw})
				if(res.meta.status !== 200) return uni.$showMsg()
				this.queryList = res.message
			},
			saveSearchHistory(){
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				set.delete(this.kw)
				if(this.kw.length === 0) return
				set.add(this.kw)
				this.historyList = Array.from(set)
				uni.setStorageSync("kw",this.historyList)
			},
			clearHistory(){
				this.historyList = []
				uni.removeStorageSync("kw")
			},
			gotoGoodsDetail(item){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id="+item.goods_id
				})
			}
		},
		computed:{
			historys(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
.search-list{
	padding: 0 10px;
	.search-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		.search-goods-name{
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
}
.search-history-list{
	padding: 0 5px;
	height: 100px;
		.history-title{
		height: 53px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #efefef;
		margin-bottom: 10px;
		.title-text{
			font-size: 12px;
		}
	}
	.history-items{
		display: flex;
		flex-wrap: wrap;
		padding: 0 5px;
		.uni-tag{
				display: block;
				background-color: #e1e1e1;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				max-width: 120px;
				border: none;
				color: black;
				margin-right: 10px;
				padding: 4px 10px;
				margin-bottom: 5px;
		}
	}
}
</style>
